<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <title>Part.2.D.6-modules</title>
        <link rel="stylesheet" href="/theme/css/main.css" />

        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
</head>

<body id="index" class="home">
        <header id="banner" class="body">
                <h1><a href="/">PythonTutorials </a></h1>
                <nav><ul>
                    <li><a href="/category/cpp.html">cpp</a></li>
                    <li class="active"><a href="/category/python.html">python</a></li>
                </ul>
                </nav>
        </header><!-- /#banner -->
<section id="content" class="body">
  <article>
    <header>
      <h1 class="entry-title">
        <a href="/Part.2.D.6-modules.html" rel="bookmark"
           title="Permalink to Part.2.D.6-modules">Part.2.D.6-modules</a></h1>
    </header>

    <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/python.html">python</a><a href="/tag/progarmming.html">progarmming</a><a href="/tag/excel.html">Excel</a></span>
</footer><!-- /.post-info -->      <h1>保存到文件的函数</h1>
<p>写好的函数，当然最好保存起来，以便将来随时调用。</p>
<h2>模块</h2>
<p>我们可以将以下内容保存到一个名为 <code>mycode.py</code> 的文件中 —— 这样可以被外部调用的 <code>.py</code> 文件，有个专门的称呼，<strong>模块</strong>（Module）—— 于是，它（任何一个 <code>.py</code> 文件）也可以被称为<em>模块</em>：</p>
<div class="highlight"><pre><span></span><span class="c1"># %load mycode.py</span>
<span class="c1"># 当前这个 Code Cell 中的代码，保存在当前文件夹中的 mycode.py 文件中</span>
<span class="c1"># 以下的代码，是使用 Jupyter 命令 %load mycode.py 导入到当前 Code Cell 中的：</span>

<span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a boolean value based upon</span>
<span class="sd">    whether the argument n is a prime number.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">True</span>
    <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="o">**</span><span class="mf">0.5</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">%</span> <span class="n">m</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">True</span>

<span class="k">def</span> <span class="nf">say_hi</span><span class="p">(</span><span class="o">*</span><span class="n">names</span><span class="p">,</span> <span class="n">greeting</span><span class="o">=</span><span class="s1">&#39;Hello&#39;</span><span class="p">,</span> <span class="n">capitalized</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Print a string, with a greeting to everyone.</span>
<span class="sd">    :param *names: tuple of names to be greeted.</span>
<span class="sd">    :param greeting: &#39;Hello&#39; as default.</span>
<span class="sd">    :param capitalized: Whether name should be converted to capitalized before print. False as default.</span>
<span class="sd">    :returns: None</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">capitalized</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span>
        <span class="k">print</span><span class="p">(</span><span class="n">f</span><span class="s1">&#39;{greeting}, {name}!&#39;</span><span class="p">)</span>
</pre></div>


<p>而后，我们就可以在其它地方这样使用（以上代码现在已经保存在当前工作目录中的 <code>mycode.py</code>）：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="kn">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s2">&quot;all&quot;</span>

<span class="kn">import</span> <span class="nn">mycode</span>

<span class="n">help</span><span class="p">(</span><span class="n">mycode</span><span class="o">.</span><span class="n">is_prime</span><span class="p">)</span>
<span class="n">help</span><span class="p">(</span><span class="n">mycode</span><span class="o">.</span><span class="n">say_hi</span><span class="p">)</span>

<span class="n">mycode</span><span class="o">.</span><span class="vm">__name__</span>
<span class="n">mycode</span><span class="o">.</span><span class="n">is_prime</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">mycode</span><span class="o">.</span><span class="n">say_hi</span><span class="p">(</span><span class="s1">&#39;mike&#39;</span><span class="p">,</span> <span class="s1">&#39;zoe&#39;</span><span class="p">)</span>
</pre></div>


<div class="highlight"><pre><span></span>Help on function is_prime in module mycode:

is_prime(n)
    Return a boolean value based upon
    whether the argument n is a prime number.

Help on function say_hi in module mycode:

say_hi(*names, greeting=&#39;Hello&#39;, capitalized=False)
    Print a string, with a greeting to everyone.
    :param *names: tuple of names to be greeted.
    :param greeting: &#39;Hello&#39; as default.
    :param capitalized: Whether name should be converted to capitalzed before print. False as default.
    :returns: None

&#39;mycode&#39;

True

Hello, mike!
Hello, zoe!
</pre></div>


<p>以上这个<strong>模块</strong>（<a href="https://docs.python.org/3/tutorial/modules.html">Module</a>）的名称，就是 <code>mycode</code>。</p>
<h2>模块文件系统目录检索顺序</h2>
<p>当你向 Python 说 <code>import ...</code> 的时候，它要去寻找你所指定的文件，那个文件应该是 <code>import</code> 语句后面引用的名称，再加上 <code>.py</code> 构成的名字的文件。Python 会按照以下顺序去寻找：</p>
<blockquote>
<ul>
<li>先去看看内建模块里有没有你所指定的名称；</li>
<li>如果没有，那么就按照 <code>sys.path</code> 所返回的目录列表顺序去找。</li>
</ul>
</blockquote>
<p>你可以通过以下代码查看你自己当前机器的 <code>sys.path</code>：</p>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span>
</pre></div>


<p>在 <code>sys.path</code> 所返回的目录列表中，你当前的工作目录排在第一位。</p>
<p>有时，你需要指定检索目录，因为你知道你要用的模块文件在什么位置，那么可以用 <code>sys.path.append()</code> 添加一个搜索位置：</p>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;/My/Path/To/Module/Directory&quot;</span><span class="p">)</span>
<span class="kn">import</span> <span class="nn">my_module</span>
</pre></div>


<h2>系统内建的模块</h2>
<p>你可以用以下代码获取系统内建模块的列表：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="kn">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s2">&quot;all&quot;</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="n">sys</span><span class="o">.</span><span class="n">builtin_module_names</span>
<span class="s2">&quot;_sre&quot;</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">builtin_module_names</span> <span class="c1"># True</span>
<span class="s2">&quot;math&quot;</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">builtin_module_names</span> <span class="c1"># False （根据自己电脑库的安装情况，结果会有不同）</span>
</pre></div>


<div class="highlight"><pre><span></span>(&#39;_abc&#39;,
 &#39;_ast&#39;,
 &#39;_codecs&#39;,
 &#39;_collections&#39;,
 &#39;_functools&#39;,
 &#39;_imp&#39;,
 &#39;_io&#39;,
 &#39;_locale&#39;,
 &#39;_operator&#39;,
 &#39;_signal&#39;,
 &#39;_sre&#39;,
 &#39;_stat&#39;,
 &#39;_string&#39;,
 &#39;_symtable&#39;,
 &#39;_thread&#39;,
 &#39;_tracemalloc&#39;,
 &#39;_warnings&#39;,
 &#39;_weakref&#39;,
 &#39;atexit&#39;,
 &#39;builtins&#39;,
 &#39;errno&#39;,
 &#39;faulthandler&#39;,
 &#39;gc&#39;,
 &#39;itertools&#39;,
 &#39;marshal&#39;,
 &#39;posix&#39;,
 &#39;pwd&#39;,
 &#39;sys&#39;,
 &#39;time&#39;,
 &#39;xxsubtype&#39;,
 &#39;zipimport&#39;)
True
False
</pre></div>


<p>跟变量名、函数名，不能与关键字重名一样，你的模块名称也最好别与系统内建模块名称重合。</p>
<h2>引入指定模块中的特定函数</h2>
<p>当你使用 <code>import mycode</code> 的时候，你向当前工作空间引入了 <code>mycode</code> 文件中定义的所有函数，相当于：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mycode</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>


<p>你其实可以只引入当前需要的函数，比如，只引入 <code>is_prime()</code>：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mycode</span> <span class="kn">import</span> <span class="n">is_prime</span>
</pre></div>


<p>这种情况下，你就不必使用 <code>mycode.is_prime()</code> 了；而是就好像这个函数就写在当前工作空间一样，直接写 <code>is_prime()</code>：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mycode</span> <span class="kn">import</span> <span class="n">is_prime</span>
<span class="n">is_prime</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
</pre></div>


<div class="highlight"><pre><span></span>True
</pre></div>


<p>注意，如果当前目录中并没有 <code>mycode.py</code> 这个文件，那么，<code>mycode</code> 会被当作目录名再被尝试一次 —— 如果当前目录内，有个叫做 <code>mycode</code> 的目录（或称文件夹）且该目录下同时要存在一个 <a href="https://docs.python.org/3/reference/import.html#regular-packages"><code>__init__.py</code></a> 文件（通常为空文件，用于标识本目录形成一个包含多个模块的 <strong>包</strong>（<a href="https://docs.python.org/3/reference/import.html#regular-packages">packages</a>），它们处在一个独立的 <strong>命名空间</strong>（<a href="https://docs.python.org/3/glossary.html#term-namespace-package">namespace</a>）），那么，<code>from mycode import *</code> 的作用就是把 <code>mycode</code> 这个文件夹中的所有 <code>.py</code> 文件全部导入……</p>
<p>如果我们想要导入 <code>foo</code> 这个目录中的 <code>bar.py</code> 这个模块文件，那么，可以这么写：</p>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">foo.bar</span>
</pre></div>


<p>或者</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">foo</span> <span class="kn">import</span> <span class="n">bar</span>
</pre></div>


<h2>引入并使用化名</h2>
<p>有的时候，或者为了避免混淆，或者为了避免输入太多字符，我们可以为引入的函数设定 <strong>化名</strong>（alias），而后使用化名调用函数。比如：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mycode</span> <span class="kn">import</span> <span class="n">is_prime</span> <span class="k">as</span> <span class="n">isp</span>
<span class="n">isp</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
</pre></div>


<div class="highlight"><pre><span></span>True
</pre></div>


<p>甚至干脆给整个模块取个化名：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="kn">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s2">&quot;all&quot;</span>

<span class="kn">import</span> <span class="nn">mycode</span> <span class="kn">as</span> <span class="nn">m</span>

<span class="n">m</span><span class="o">.</span><span class="n">is_prime</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="n">m</span><span class="o">.</span><span class="n">say_hi</span><span class="p">(</span><span class="s1">&#39;mike&#39;</span><span class="p">,</span> <span class="s1">&#39;zoe&#39;</span><span class="p">)</span>
</pre></div>


<div class="highlight"><pre><span></span>True
Hello, mike!
Hello, zoe!
</pre></div>


<h2>模块中不一定只有函数</h2>
<p>一个模块文件中，不一定只包含函数；它也可以包含函数之外的可执行代码。只不过，在 <code>import</code> 语句执行的时候，模块中的非函数部分的可执行代码，只执行一次。</p>
<p>有一个 Python 的彩蛋，恰好是可以用在此处的最佳例子 —— 这个模块是 <code>this</code>，它的文件名是 <a href="https://github.com/python/cpython/blob/master/Lib/this.py"><code>this.py</code></a>：</p>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">this</span>
</pre></div>


<div class="highlight"><pre><span></span><span class="n">The</span> <span class="n">Zen</span> <span class="k">of</span> <span class="n">Python</span><span class="p">,</span> <span class="n">by</span> <span class="n">Tim</span> <span class="n">Peters</span>

<span class="n">Beautiful</span> <span class="ow">is</span> <span class="n">better</span> <span class="n">than</span> <span class="n">ugly</span><span class="p">.</span>
<span class="n">Explicit</span> <span class="ow">is</span> <span class="n">better</span> <span class="n">than</span> <span class="n">implicit</span><span class="p">.</span>
<span class="n">Simple</span> <span class="ow">is</span> <span class="n">better</span> <span class="n">than</span> <span class="n">complex</span><span class="p">.</span>
<span class="n">Complex</span> <span class="ow">is</span> <span class="n">better</span> <span class="n">than</span> <span class="n">complicated</span><span class="p">.</span>
<span class="n">Flat</span> <span class="ow">is</span> <span class="n">better</span> <span class="n">than</span> <span class="n">nested</span><span class="p">.</span>
<span class="n">Sparse</span> <span class="ow">is</span> <span class="n">better</span> <span class="n">than</span> <span class="n">dense</span><span class="p">.</span>
<span class="n">Readability</span> <span class="n">counts</span><span class="p">.</span>
<span class="n">Special</span> <span class="n">cases</span> <span class="n">aren</span><span class="c">&#39;t special enough to break the rules.</span>
<span class="n">Although</span> <span class="n">practicality</span> <span class="n">beats</span> <span class="n">purity</span><span class="p">.</span>
<span class="n">Errors</span> <span class="n">should</span> <span class="n">never</span> <span class="n">pass</span> <span class="n">silently</span><span class="p">.</span>
<span class="n">Unless</span> <span class="n">explicitly</span> <span class="n">silenced</span><span class="p">.</span>
<span class="ow">In</span> <span class="n">the</span> <span class="n">face</span> <span class="k">of</span> <span class="n">ambiguity</span><span class="p">,</span> <span class="n">refuse</span> <span class="n">the</span> <span class="n">temptation</span> <span class="k">to</span> <span class="n">guess</span><span class="p">.</span>
<span class="n">There</span> <span class="n">should</span> <span class="n">be</span> <span class="n">one</span><span class="o">--</span> <span class="ow">and</span> <span class="n">preferably</span> <span class="n">only</span> <span class="n">one</span> <span class="o">--</span><span class="n">obvious</span> <span class="n">way</span> <span class="k">to</span> <span class="k">do</span> <span class="n">it</span><span class="p">.</span>
<span class="n">Although</span> <span class="n">that</span> <span class="n">way</span> <span class="n">may</span> <span class="k">not</span> <span class="n">be</span> <span class="n">obvious</span> <span class="n">at</span> <span class="n">first</span> <span class="n">unless</span> <span class="n">you</span><span class="c">&#39;re Dutch.</span>
<span class="n">Now</span> <span class="ow">is</span> <span class="n">better</span> <span class="n">than</span> <span class="n">never</span><span class="p">.</span>
<span class="n">Although</span> <span class="n">never</span> <span class="ow">is</span> <span class="n">often</span> <span class="n">better</span> <span class="n">than</span> <span class="o">*</span><span class="n">right</span><span class="o">*</span> <span class="n">now</span><span class="p">.</span>
<span class="k">If</span> <span class="n">the</span> <span class="n">implementation</span> <span class="ow">is</span> <span class="n">hard</span> <span class="k">to</span> <span class="n">explain</span><span class="p">,</span> <span class="n">it</span><span class="c">&#39;s a bad idea.</span>
<span class="k">If</span> <span class="n">the</span> <span class="n">implementation</span> <span class="ow">is</span> <span class="n">easy</span> <span class="k">to</span> <span class="n">explain</span><span class="p">,</span> <span class="n">it</span> <span class="n">may</span> <span class="n">be</span> <span class="n">a</span> <span class="n">good</span> <span class="n">idea</span><span class="p">.</span>
<span class="n">Namespaces</span> <span class="n">are</span> <span class="n">one</span> <span class="n">honking</span> <span class="n">great</span> <span class="n">idea</span> <span class="o">--</span> <span class="k">let</span><span class="c">&#39;s do more of those!</span>
</pre></div>


<p>这个 <code>this</code> 模块中的代码如下：</p>
<div class="highlight"><pre><span></span><span class="n">s</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;Gur Mra bs Clguba, ol Gvz Crgref</span>
<span class="s2">Ornhgvshy vf orggre guna htyl.</span>
<span class="s2">Rkcyvpvg vf orggre guna vzcyvpvg.</span>
<span class="s2">Fvzcyr vf orggre guna pbzcyrk.</span>
<span class="s2">Pbzcyrk vf orggre guna pbzcyvpngrq.</span>
<span class="s2">Syng vf orggre guna arfgrq.</span>
<span class="s2">Fcnefr vf orggre guna qrafr.</span>
<span class="s2">Ernqnovyvgl pbhagf.</span>
<span class="s2">Fcrpvny pnfrf nera&#39;g fcrpvny rabhtu gb oernx gur ehyrf.</span>
<span class="s2">Nygubhtu cenpgvpnyvgl orngf chevgl.</span>
<span class="s2">Reebef fubhyq arire cnff fvyragyl.</span>
<span class="s2">Hayrff rkcyvpvgyl fvyraprq.</span>
<span class="s2">Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.</span>
<span class="s2">Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.</span>
<span class="s2">Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh&#39;er Qhgpu.</span>
<span class="s2">Abj vf orggre guna arire.</span>
<span class="s2">Nygubhtu arire vf bsgra orggre guna *evtug* abj.</span>
<span class="s2">Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg&#39;f n onq vqrn.</span>
<span class="s2">Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.</span>
<span class="s2">Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg&#39;f qb zber bs gubfr!&quot;&quot;&quot;</span>

<span class="n">d</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">65</span><span class="p">,</span> <span class="mi">97</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">26</span><span class="p">):</span>
        <span class="n">d</span><span class="p">[</span><span class="nb">chr</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="n">c</span><span class="p">)]</span> <span class="o">=</span> <span class="nb">chr</span><span class="p">((</span><span class="n">i</span><span class="o">+</span><span class="mi">13</span><span class="p">)</span> <span class="o">%</span> <span class="mi">26</span> <span class="o">+</span> <span class="n">c</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">d</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">s</span><span class="p">]))</span>
</pre></div>


<p>这个 <code>this.py</code> 文件中也没有什么函数，但这个文件里所定义的变量，我们都可以在 <code>import this</code> 之后触达：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="kn">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s2">&quot;all&quot;</span>

<span class="kn">import</span> <span class="nn">this</span>
<span class="n">this</span><span class="o">.</span><span class="n">d</span>
<span class="n">this</span><span class="o">.</span><span class="n">s</span>
</pre></div>


<div class="highlight"><pre><span></span>{&#39;A&#39;: &#39;N&#39;,
 &#39;B&#39;: &#39;O&#39;,
 &#39;C&#39;: &#39;P&#39;,
 &#39;D&#39;: &#39;Q&#39;,
 &#39;E&#39;: &#39;R&#39;,
 &#39;F&#39;: &#39;S&#39;,
 &#39;G&#39;: &#39;T&#39;,
 &#39;H&#39;: &#39;U&#39;,
 &#39;I&#39;: &#39;V&#39;,
 &#39;J&#39;: &#39;W&#39;,
 &#39;K&#39;: &#39;X&#39;,
 &#39;L&#39;: &#39;Y&#39;,
 &#39;M&#39;: &#39;Z&#39;,
 &#39;N&#39;: &#39;A&#39;,
 &#39;O&#39;: &#39;B&#39;,
 &#39;P&#39;: &#39;C&#39;,
 &#39;Q&#39;: &#39;D&#39;,
 &#39;R&#39;: &#39;E&#39;,
 &#39;S&#39;: &#39;F&#39;,
 &#39;T&#39;: &#39;G&#39;,
 &#39;U&#39;: &#39;H&#39;,
 &#39;V&#39;: &#39;I&#39;,
 &#39;W&#39;: &#39;J&#39;,
 &#39;X&#39;: &#39;K&#39;,
 &#39;Y&#39;: &#39;L&#39;,
 &#39;Z&#39;: &#39;M&#39;,
 &#39;a&#39;: &#39;n&#39;,
 &#39;b&#39;: &#39;o&#39;,
 &#39;c&#39;: &#39;p&#39;,
 &#39;d&#39;: &#39;q&#39;,
 &#39;e&#39;: &#39;r&#39;,
 &#39;f&#39;: &#39;s&#39;,
 &#39;g&#39;: &#39;t&#39;,
 &#39;h&#39;: &#39;u&#39;,
 &#39;i&#39;: &#39;v&#39;,
 &#39;j&#39;: &#39;w&#39;,
 &#39;k&#39;: &#39;x&#39;,
 &#39;l&#39;: &#39;y&#39;,
 &#39;m&#39;: &#39;z&#39;,
 &#39;n&#39;: &#39;a&#39;,
 &#39;o&#39;: &#39;b&#39;,
 &#39;p&#39;: &#39;c&#39;,
 &#39;q&#39;: &#39;d&#39;,
 &#39;r&#39;: &#39;e&#39;,
 &#39;s&#39;: &#39;f&#39;,
 &#39;t&#39;: &#39;g&#39;,
 &#39;u&#39;: &#39;h&#39;,
 &#39;v&#39;: &#39;i&#39;,
 &#39;w&#39;: &#39;j&#39;,
 &#39;x&#39;: &#39;k&#39;,
 &#39;y&#39;: &#39;l&#39;,
 &#39;z&#39;: &#39;m&#39;}

&quot;Gur Mra bs Clguba, ol Gvz Crgref\n\nOrnhgvshy vf orggre guna htyl.\nRkcyvpvg vf orggre guna vzcyvpvg.\nFvzcyr vf orggre guna pbzcyrk.\nPbzcyrk vf orggre guna pbzcyvpngrq.\nSyng vf orggre guna arfgrq.\nFcnefr vf orggre guna qrafr.\nErnqnovyvgl pbhagf.\nFcrpvny pnfrf nera&#39;g fcrpvny rabhtu gb oernx gur ehyrf.\nNygubhtu cenpgvpnyvgl orngf chevgl.\nReebef fubhyq arire cnff fvyragyl.\nHayrff rkcyvpvgyl fvyraprq.\nVa gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.\nGurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.\nNygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh&#39;er Qhgpu.\nAbj vf orggre guna arire.\nNygubhtu arire vf bsgra orggre guna *evtug* abj.\nVs gur vzcyrzragngvba vf uneq gb rkcynva, vg&#39;f n onq vqrn.\nVs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.\nAnzrfcnprf ner bar ubaxvat terng vqrn -- yrg&#39;f qb zber bs gubfr!&quot;
</pre></div>


<p>试试吧，试试能否独立读懂这个文件里的代码 —— 对初学者来说，还是挺练脑子的呢！</p>
<p>它先是通过一个规则生成了一个密码表，保存在 <code>d</code> 这个字典中；而后，将 <code>s</code> 这个变量中保存的 “密文” 翻译成了英文……</p>
<p>或许，你可以试试，看看怎样能写个函数出来，给你一段英文，你可以把它加密成跟它一样的 “密文”？</p>
<h2>dir() 函数</h2>
<p>你的函数，保存在模块里之后，这个函数的用户（当然也包括你），可以用 <code>dir()</code> 函数查看模块中可触达的变量名称和函数名称：</p>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mycode</span>
<span class="nb">dir</span><span class="p">(</span><span class="n">mycode</span><span class="p">)</span>
</pre></div>


<div class="highlight"><pre><span></span>[&#39;__builtins__&#39;,
 &#39;__cached__&#39;,
 &#39;__doc__&#39;,
 &#39;__file__&#39;,
 &#39;__loader__&#39;,
 &#39;__name__&#39;,
 &#39;__package__&#39;,
 &#39;__spec__&#39;,
 &#39;is_prime&#39;,
 &#39;say_hi&#39;]
</pre></div>
    </div><!-- /.entry-content -->

  </article>
</section>
        <section id="extras" class="body">
                <div class="blogroll">
                        <h2>blogroll</h2>
                        <ul>
                            <li><a href="http://getpelican.com/">Pelican</a></li>
                            <li><a href="http://python.org/">Python.org</a></li>
                            <li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
                            <li><a href="#">You can modify those links in your config file</a></li>
                        </ul>
                </div><!-- /.blogroll -->
                <div class="social">
                        <h2>social</h2>
                        <ul>

                            <li><a href="#">You can add links in your config file</a></li>
                            <li><a href="#">Another social link</a></li>
                        </ul>
                </div><!-- /.social -->
        </section><!-- /#extras -->

        <footer id="contentinfo" class="body">
                <p>Powered by <a href="http://www.maxiaoan.com/">Maxiaoan</a></p>
        </footer><!-- /#contentinfo -->

</body>
</html>